package com.app.service.Impl;

import com.app.common.CalculateDistance;
import com.app.mapper.GobiMapper;
import com.app.model.CategoryModel;
import com.app.model.GobiModel;
import com.app.model.TownshipModel;
import com.app.service.CategoryService;
import com.app.service.GobiService;
import com.app.service.TownService;
import com.app.vo.GobiVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

@Service
public class GobiServiceImpl implements GobiService {
    @Autowired
    private GobiMapper gobiMapper;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private TownService townService;
    @Override
    public void update(GobiModel model) {
        gobiMapper.updateById(model);
    }

    @Override
    public void insert(GobiModel model) {
        gobiMapper.insert(model);
    }

    @Override
    public void deleteById(Integer id) {
        gobiMapper.deleteById(id);
    }

    @Override
    public GobiVo selectList(Integer page, Integer limit, String keyword, Integer type, Double longitude, Double latitude,Integer township) {
        GobiVo vo = new GobiVo();
        page = (page - 1) * limit;
        List<GobiModel> gobiList = gobiMapper.getGobiList(page, limit, keyword, type,township);
        for (int i = 0; i < gobiList.size(); i++) {
            if (gobiList.get(i).getType() != null) {
                CategoryModel categoryById = categoryService.getCategoryById(gobiList.get(i).getType());
                gobiList.get(i).setTypeText(categoryById.getName());
            }
            String[] split = gobiList.get(i).getSwiperImage().split(",");
            gobiList.get(i).setSwiperImages(Arrays.asList(split));
            if (gobiList.get(i).getLongitude() != null && gobiList.get(i).getLatitude() != null) {
                double longitude1 = Double.parseDouble(gobiList.get(i).getLongitude());
                double latitude1 = Double.parseDouble(gobiList.get(i).getLatitude());

                if (latitude != null && longitude != null) {
                    Double haversine = CalculateDistance.haversine(latitude1, longitude1, latitude, longitude);
                    if (haversine != 0) {
                        DecimalFormat df = new DecimalFormat("0.0");
                        String formatted = df.format(haversine);
                        gobiList.get(i).setDistance(formatted + "km");
                    }
                } else {
                    gobiList.get(i).setDistance("暂无");
                }
            } else {
                gobiList.get(i).setDistance("暂无");
            }
        }
        List<TownshipModel> townList = townService.getTownList();
        for (int i = 0; i < gobiList.size(); i++) {
            if (townList != null) {
                for (int i1 = 0; i1 < townList.size(); i1++) {
                    if (gobiList.get(i).getTownship() != null && gobiList.get(i).getTownship().equals(townList.get(i1).getId())) {
                        gobiList.get(i).setTownshipText(townList.get(i1).getTownshipName());
                    }
                }
            }
        }
        int gobiCount = gobiMapper.getGobiCount(keyword, type);
        vo.setList(gobiList);
        vo.setCount(gobiCount);
        return vo;
    }

    @Override
    public GobiVo getList(Integer page, Integer limit, String keyword, Integer type,Integer township) {
        GobiVo vo = new GobiVo();
        page = (page - 1) * limit;
        List<GobiModel> gobiList = gobiMapper.selectGobiList(page, limit, keyword, type,township);
        for (int i = 0; i < gobiList.size(); i++) {
            if (gobiList.get(i).getType() != null) {
                CategoryModel categoryById = categoryService.getCategoryById(gobiList.get(i).getType());
                gobiList.get(i).setTypeText(categoryById.getName());
            }
            String[] split = gobiList.get(i).getSwiperImage().split(",");
            gobiList.get(i).setSwiperImages(Arrays.asList(split));
        }
        List<TownshipModel> townList = townService.getTownList();
        for (int i = 0; i < gobiList.size(); i++) {
            if (townList != null) {
                for (int i1 = 0; i1 < townList.size(); i1++) {
                    if (gobiList.get(i).getTownship() != null && gobiList.get(i).getTownship().equals(townList.get(i1).getId())) {
                        gobiList.get(i).setTownshipText(townList.get(i1).getTownshipName());
                    }
                }
            }
        }
        int gobiCount = gobiMapper.selectGobiCount(keyword, type);
        vo.setList(gobiList);
        vo.setCount(gobiCount);
        return vo;
    }

    @Override
    public GobiModel selectById(Integer id) {
        GobiModel gobiModel = gobiMapper.selectById(id);
        if (gobiModel.getType()!=null){
            CategoryModel categoryById = categoryService.getCategoryById(gobiModel.getType());
            gobiModel.setTypeText(categoryById.getName());
        }
        String[] split = gobiModel.getSwiperImage().split(",");
        gobiModel.setSwiperImages(Arrays.asList(split));
        return gobiModel;
    }
}
